package com.bjsxt.commons;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class JdbcUtils {
    private static String driver;
    private static String username;
    private static String pwd;
    private static String url;

   static {
        try{
            //获取db.properties文件的数据
            //db.properties  不要扩展名
            ResourceBundle bundle = ResourceBundle.getBundle("db");
            //根据key-value 读取
            driver=bundle.getString("driver");
            username = bundle.getString("username");
            pwd= bundle.getString("pwd");
            url=bundle.getString("url");
            //加载数据库驱动
            Class.forName(driver);
        }catch (Exception e){
            e.printStackTrace();
        }
   }


    //获取连接方法
    public static Connection getConnection(){
       Connection conn = null;
       try{
           conn=DriverManager.getConnection(url,username,pwd);

       }catch (Exception e){
           e.printStackTrace();
       }
        return conn;
    }

    //关闭连接方法
    public static void closeConnection(Connection conn){
        try {
            conn.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    //事业回滚
    public static void rollbackConnection(Connection conn){
        try {
            conn.rollback();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
